package com.itextStudy;

import java.util.*;

import com.itextpdf.awt.geom.Rectangle2D;
import com.itextpdf.awt.geom.RectangularShape;
import com.itextpdf.text.pdf.parser.*;

/**
 * pdf文档处理
 */
public class TestRenderListener implements RenderListener {

    // 关键字
    public String keyWord;
    // 关键字字符数组
    public char[] charArray;
    // PDF当前页数
    public int curPage;
    // 坐标信息集合
    public List<Entity> result = new ArrayList<>();
    //下标
    public int subscript = 0;
    //是否找到
    public boolean isFind = false;

    Entity entity;

    public TestRenderListener(int curPage, String keyWord) {
        this.curPage = curPage;
        this.keyWord = keyWord;
        this.charArray = keyWord.toCharArray();
    }

    /**
     * 文字主要处理方法
     */
    @Override
    public void renderText(TextRenderInfo textRenderInfo) {
        String text = textRenderInfo.getText();
        RectangularShape rectBase = textRenderInfo.getBaseline().getBoundingRectange();
        Rectangle2D.Float rectAscen = textRenderInfo.getAscentLine().getBoundingRectange();
        //查找是否匹配 关键字的第一个字符
        if (text.equals(keyWord.substring(0, 1))) {
            Rectangle2D.Float start = new Rectangle2D.Float();
            start.setRect((float) rectBase.getMinX()
                    , (float) rectBase.getMinY()
                    , (float) (rectAscen.getMaxX() - rectBase.getMinX())
                    , (float) (rectAscen.getMaxY() - rectBase.getMinY())
            );
            entity = new Entity();
            entity.setPageNumber(curPage);
            entity.setStart(start);

            //下标自增
            subscript++;
            //已找到
            isFind = true;

            return;
        }

        if (isFind) {
            //下标不能超过关键字的长度
            if (subscript < charArray.length) {
                //判断后续的字符是否符合
                if (text.equals(String.valueOf(charArray[subscript]))) {
                    //下标自增
                    subscript++;

                    if (subscript >= charArray.length){
                        Rectangle2D.Float end = new Rectangle2D.Float();

                        end.setRect((float) rectBase.getMinX()
                                , (float) rectBase.getMinY()
                                , (float) (rectAscen.getMaxX() - rectBase.getMinX())
                                , (float) (rectAscen.getMaxY() - rectBase.getMinY())
                        );
                        entity.setEnd(end);
                        entity.setSign(keyWord);
                        result.add(entity);
                        System.out.println("成功-" + keyWord + "-" + text + "-" + charArray[subscript-1] + "-" + subscript);
                        //这里是找完一组了 所以重置数据
                        isFind = false;
                        subscript = 0;
                        return;
                    }

                    return;
                }
                //如果后续字符不匹配
                else {
                    System.out.println("失败-" + keyWord + "-" + text + "-" + charArray[subscript] + "-" + subscript);
                    isFind = false;
                    subscript = 0;
                    return;
                }
            }
        }
    }

    @Override
    public void beginTextBlock() {
    }

    @Override
    public void endTextBlock() {
    }

    //step 1(图片处理方法)
    @Override
    public void renderImage(ImageRenderInfo renderInfo) {

    }
}